<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-07-08 12:33:50
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\BaseData;

use App\Http\Services\Service;
use App\Repositories\LoomDetailsRepository;
use App\Repositories\LoomRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property LoomDetailsRepository $detail
 * @property LoomRepository $repository
 */
class LoomService extends Service
    {

    public function __construct(LoomRepository $repository, LoomDetailsRepository $detail)
        {
        $this->repository = $repository;
        $this->detail     = $detail;
        }

    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function info()
        {
        return $this->repository->setRelation(['details' => ['category'], 'ratios' => ['category']])->condition(request())->getOne();
        }
    public function fetchRatio()
        {
        return $this->repository->fetchRatio();
        }
    protected function save($request)
        {
        return $this->repository->autosave($request->toArray(), false, ['details', 'ratios']);
        }
    protected function delete()
        {
        return $this->repository->with(['details'])->autodelete($this->getRequestIds());
        }
    public function details()
        {
        return $this->detail->condition(request())->getGrid();
        }
    public function sync()
        {
        $data = app("ZhiJing")->Device->query();
        $res  = [];
        foreach ($data['data'] as $v) {
            $gdata = $this->repository->whereCode($v['deviceName'])->first();
            if ($gdata) {
                $gdata->device_id                 = $v['id'];
                $gdata->dyeing_machine_model_code = $v['dyeingMachineModelCode'];
                $res[]                            = $gdata;
                $gdata->save();
                }
            }
        return $res;
        }
    }